package com.sz.sensor.module.platform;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sz.framework.base.CommonLazyFragment;
import com.sz.framework.widget.TitleManager;
import com.sz.sensor.R;
import com.sz.sensor.adapter.PlatformAdapter;
import com.sz.sensor.entity.MeEntity;
import com.sz.sensor.module.platform.presenter.PlatformPresenter;
import com.sz.sensor.module.platform.view.PlatformView;

import java.util.List;

import butterknife.BindView;

public class PlatformFragment extends CommonLazyFragment implements PlatformView {
    @BindView(R.id.tm_title)
    TitleManager tmTitle;
    @BindView(R.id.rv_content)
    RecyclerView rvContent;

    private PlatformPresenter presenter;
    private PlatformAdapter platformAdapter;

    @Override
    public int generateLayout() {
        return R.layout.fragment_platform;
    }

    public static PlatformFragment newInstance() {
        return new PlatformFragment();
    }

    @Override
    public void initTitleBar() {
        super.initTitleBar();
        tmTitle.showTitle(R.string.management_title);
        tmTitle.showRight(R.drawable.icon_add, v -> showToastMessage("添加"));
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        presenter = new PlatformPresenter();
        presenter.setViewer(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        platformAdapter = new PlatformAdapter();
        rvContent.setLayoutManager(linearLayoutManager);
        rvContent.setAdapter(platformAdapter);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getPlatformData();
    }

    @Override
    public void getPlatformData() {
        presenter.getPlatformData();
    }

    @Override
    public void onGetPlatformData(List<MeEntity> entities) {
        platformAdapter.setNewData(entities);
    }
}
